// comm模块

#pragma once

#include <iostream>
#include <string>
#include <atomic>
#include <fstream>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>

namespace dsh_util
{
    const std::string temp_path = "./temp/";
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string& file_name, const std::string& suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;
            return path_name;                                                                                                                                                                                                                                                                                                                                                                     
        }

        // 编译时形成的临时文件
        // 构建源文件路径+后缀
        static std::string Src(const std::string& file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        // 构建可执行程序路径+后缀
        static std::string Exe(const std::string& file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        // 构建运行时错误路径+后缀
        static std::string CompilerError(const std::string& file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }

        // 运行时形成的临时文件
        // 构建标准输入路径+后缀
        static std::string Stdin(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        // 构建标准输出路径+后缀
        static std::string Stdout(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        // 构建标准错误路径+后缀
        static std::string Stderr(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec);
        }

        static std::string GetTimeMs() // 获得ms级时间戳
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec * 1000 + tv.tv_usec / 1000); // 将秒和微秒转化为毫秒
        }
    };

    class FileUtil
    {
    public:
        static bool isFileExits(const std::string& path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            { 
                // 获取文件属性成功，文件已经存在
                return true;
            }
            else
                return false;
        }

        static std::string UniqueFileName()
        {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetTimeMs();
            std::string uid = std::to_string(id);
            return ms + "_" + uid;
        }

        static bool WriteFile(const std::string& target, const std::string& content)
        {
            std::ofstream out(target);
            if (!out.is_open()) return false;
            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string& target, std::string* content, bool keep)
        {
            std::ifstream in(target);
            if (!in.is_open()) return false;
            std::string line;
            while (std::getline(in, line))
            {
                (*content) += line;
                (*content) += (keep ? "\n":"");
            }
            in.close();
            return true;
        }
    };
}